import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:squat_easy/se_constant.dart';
import 'package:squat_easy/se_login/se_login_page.dart';
import 'package:squat_easy/se_main/se_main_page.dart';
import 'package:squat_easy/se_service/se_log_service.dart';
import 'package:squat_easy/se_webview/se_web_page.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class SeRoute {
  ///获取入口
  static Widget getRoot(String token) {
    bool isLogin = token.notEmpty;
    return isLogin ? const SeMainPage() : const SeLoginPage();
  }

  ///关闭路由
  static close() => Get.back();

  ///打开登录
  static void openLogin() {
    _openRoot(const SeLoginPage());
  }

  ///打开首页
  static void openMain() {
    _openRoot(const SeMainPage());
  }

  /// 打开用户协议
  static void openProtocolPage() {
    open(
      const SeWebPage(
        url: SeConstantValues.agreementUrl,
        title: 'Term of Use',
      ),
    );
    SeLogManager.stat(page: SeLogPage.terms);
  }

  /// 打开隐私政策
  static void openPrivacyPage() {
    open(
      const SeWebPage(
        url: SeConstantValues.privacyUrl,
        title: 'Privacy Policy',
      ),
    );
    SeLogManager.stat(page: SeLogPage.privacy);
  }

  ///打开某个界面并关闭所有
  static void _openRoot<T>(T page) {
    Get.offAll(() => page);
  }

  static void open<T>(
    T page, {
    bool preventDuplicates = false,
    VoidCallback? back,
    Transition? transition,
    Duration? duration,
    bool fullscreenDialog = false,
  }) {
    Get.to(
      () => page,
      preventDuplicates: preventDuplicates,
      transition: transition,
      duration: duration,
      fullscreenDialog: fullscreenDialog,
    )?.whenComplete(() => back?.call());
  }

  static void offAll<T>(
    T page, {
    bool preventDuplicates = false,
    VoidCallback? back,
    Transition? transition,
    Duration? duration,
    bool fullscreenDialog = false,
  }) {
    Get.offAll(
      () => page,
      transition: transition,
      duration: duration,
      fullscreenDialog: fullscreenDialog,
    )?.whenComplete(() => back?.call());
  }

  static void until(
    String pageName, {
    bool preventDuplicates = false,
    VoidCallback? back,
    Transition? transition,
    Duration? duration,
    bool fullscreenDialog = false,
  }) {
    Get.until((route) => route.settings.name == pageName);
  }
}
